local hx__zhaocai = fk.CreateSkill {
  name = "hx__zhaocai",
}

hx__zhaocai:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__zhaocai.name) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          if move.from == player then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                return player:getHandcardNum() < player:getLostHp()
              end
            end
          end
          if move.to == player and move.toArea == Card.PlayerHand then
            return player:getHandcardNum() < player:getLostHp()
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askToChoosePlayers(player,{
      targets = player.room:getAlivePlayers(),
      min_num = 1,
      max_num = 1,
      prompt = "#hx__zhaocai-choose",
      skill_name = hx__zhaocai.name,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:drawCards(player, 1, hx__zhaocai.name)
    local card = room:drawCards(to, 1, hx__zhaocai.name)
    room:setCardMark(Fk:getCardById(card[1]), "@@hx__zhaocai", 1)
    room:setCardMark(Fk:getCardById(card[1]), "hx__zhaocai_info", {player.id, to.id, 0})
  end,
})

hx__zhaocai:addEffect(fk.AfterCardsMove, {
  mute = true,
  priority = 0.9,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player and (move.moveReason == fk.ReasonUse or move.moveReason == fk.ReasonResonpse) then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("hx__zhaocai_info") ~= 0 then
            return true
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player and (move.moveReason == fk.ReasonUse or move.moveReason == fk.ReasonResonpse) then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("hx__zhaocai_info") ~= 0 then
            local card = Fk:getCardById(info.cardId)
            local mark = card:getMark("hx__zhaocai_info")
            room:setCardMark(card, "@@hx__zhaocai", 0)
            local src = room:getPlayerById(mark[1])
            if not src.dead and src:hasSkill(hx__zhaocai.name) then
              if not player.dead then
                room:changeMaxHp(player,-1)
              end
            end
            room:setCardMark(card, "hx__zhaocai_info", 0)
          end
        end
      end
    end
  end,
})

return hx__zhaocai